package com.open.capacity.reimburse.controller;

import java.util.List;
import java.util.Map;

import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.entity.HtAgreementAuthorize;
import com.open.capacity.reimburse.entity.SysFileinfoAuthorize;
import com.open.capacity.reimburse.service.HtAgreementAuthorizeService;
import com.open.capacity.reimburse.service.SysFileinfoAuthorizeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.HtAgreement;
import com.open.capacity.reimburse.service.HtAgreementService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 合同表
 *
 * @author
 * @email
 * @date 2021-02-24 18:00:18
 */
@Slf4j
@RestController
@RequestMapping("agreements")
@Api(tags = "合同表")
public class HtAgreementController {

    @Autowired
    private HtAgreementService htAgreementService;

    @Autowired
    private HtAgreementAuthorizeService htAgreementAuthorizeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('ht_agreement:htagreement:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = htAgreementService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('ht_agreement:htagreement:list')")
    public Result findById(String id) {
        return Result.succeed(htAgreementService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('ht_agreement:htagreement:save')")
    public Result save(@RequestBody @Valid HtAgreement htAgreement, String keyValue) {
        try {
            if (StringUtils.isEmpty(keyValue)) {
                htAgreementService.save(htAgreement);
            } else {
                htAgreement.setId(keyValue);
                htAgreementService.update(htAgreement);
            }
        } catch (Exception e) {
            log.error(e.toString());
            return Result.failed(e.getMessage());
        }
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('ht_agreement:htagreement:delete')")
    public Result delete(@PathVariable String id) {
        htAgreementService.delete(id);
        return Result.succeed("删除成功");
    }

    /**
     * 获取共享权限配置
     */
    @RequestMapping("/share/getPermission")
    public Result getSharePermission(String keyValue) {
        List<HtAgreementAuthorize> result = htAgreementAuthorizeService.findByAgreementId(keyValue);
        return Result.succeed(result);
    }


    /**
     * 共享
     */
    @RequestMapping("/share/{id}")
    public Result share(@PathVariable String id, @RequestBody @Valid List<HtAgreementAuthorize> agreementAuthorizes, Integer isShare) {
        htAgreementService.share(id, agreementAuthorizes, isShare);
        return Result.succeed("共享成功");
    }
}
